using hlib.IO;
using QntPlatform.Strategy;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace QntPlatform.Strategy.CmdCtrl
{
    public class CmdHandler : StrategyBase
    {
        ILog log = Log.Default();
        public CmdHandler(IExchange exchange, ISysFun sysFun, ISysApi sysApi, ITAFun ta) : base(exchange, sysFun, sysApi, ta)
        {
        }

        public T GetCtrl<T>() where T : ICmdCtrl, new()
        {
            return new T();
        }
        IDictionary<string, Func<IExchange, ICmdCtrl>> ctrlDic = new Dictionary<string, Func<IExchange, ICmdCtrl>>();
        public override void Init()
        {
            //添加路由映射：名称，对应策略
            //调用策略，传入上下文
            var ts = new TrendSet(this);
            ctrlDic.Add("trend",p=>ts);
            ctrlDic.Add("tg3", (p) => new TrailingGroup3(this));
            ctrlDic.Add("signSync", (p)=>new BuyStop(this));
            ctrlDic.Add("ts2",p=>new TrailingStep2(this));
            ctrlDic.Add("sl5", p => new SurmountLv5(this));
        }
        public override void MainFun()
        {
            //行情监测
            //命令交易
            SysFun.CommandEvent += SysFun_CommandEvent;
        }
        public override void OnExit(Exception ex = null)
        {
           SysFun.CommandEvent -= SysFun_CommandEvent;
        }
        private void SysFun_CommandEvent(object sender, string e)
        {
            //命令格式1cmdName:val,2cmdName:{}

            var cmd = GetCmdKV(e);
            if (ctrlDic.TryGetValue(cmd.Key, out var cmdFun))
            {
                var context = new CmdContext();

                context.Cmd = cmd;
                context.CmdStr = e;
                context.Add("cmd", cmd);
                context.Add("cmdStr", e);
                context.Add("CmdHandler", this);
                var fun = cmdFun(this.Exchange);
                log.Debug("命令处理对象：" + fun);
                fun.Execute(context);
            }
            else
            {
                log.Debug("无效命令:" + e);
            }

        }

        public KeyValuePair<string, string> GetCmdKV(string cmdStr)
        {
            //
            var cmdArr = cmdStr.Split(':', 2);
            string cmdVal = null;
            var cmdName = cmdArr[0];
            if (cmdArr.Length < 2)
            {
                if (cmdStr.StartsWith('{') || cmdStr.EndsWith('}'))
                {
                    var dic = SysFun.ParseTo<Dictionary<string, object>>(cmdStr);
                    if (dic.TryGetValue("cmdName", out var strName))
                    {
                        return new KeyValuePair<string, string>(strName + "", cmdStr);
                    }
                }
                return new KeyValuePair<string, string>(cmdName, cmdVal);
            }
            // var cr = new CsvReader(new StringReader(cmdStr),'~','*') ;
            //var list= cr.ReadCsv();


            cmdVal = cmdArr[1];
            return new KeyValuePair<string, string>(cmdName, cmdVal);
        }


    }
}
